package table.tools;

import java.util.ArrayList;
import java.util.List;

import table.Col;
import table.DataTable;
import table.Row;

public class TableA2B {
	/*
	 * Static APIs
	 */
	public static boolean copyColsFromAtoB(List<Col> aCols, List<Col> bCols, int rowIndex){
		try{
			for(int colIndex=0; colIndex < aCols.size(); colIndex++){
				Col aCol = aCols.get(colIndex);
				Col bCol = bCols.get(colIndex);
				bCol.clearData();
				if(rowIndex < 0 ){
					for(int i=0; i < aCol.getDataSize(); i++){
						Object aColData = aCol.getValue(i);
						bCol.addData(aCol.setNode(aColData));
					}
				} else {
					Object aColData = aCol.getValue(rowIndex);
					bCol.addData(aCol.setNode(aColData));
				}
			}
		} catch(Exception e){
			e.printStackTrace();
		}
		return false;
	}
	
	public static List<Col> getTableColsByNames(DataTable table, List<String> names){
		  List<Col> cols = new ArrayList<Col>();
		  if(names == null || table == null) return null;
		  for(int i=0; i < names.size(); i++){
			  if(table.getColByName(names.get(i)) != null){
				  cols.add(table.getColByName(names.get(i)));
			  }
		  }
		  return cols;
	  }

	private DataTable ATable;
	private DataTable BTable;
	
	private List<String> AColNames;
	private List<String> BColNames;
	
	public void setATable(DataTable aTable) {
		ATable = aTable;
	}

	public DataTable getATable() {
		return ATable;
	}

	public void setBTable(DataTable bTable) {
		BTable = bTable;
	}

	public DataTable getBTable() {
		return BTable;
	}

	public void setAColNames(List<String> aColNames) {
		AColNames = aColNames;
	}

	public List<String> getAColNames() {
		return AColNames;
	}

	public void setBColNames(List<String> bColNames) {
		BColNames = bColNames;
	}

	public List<String> getBColNames() {
		return BColNames;
	}

	/**************
	 *** APIs *****
	 **************/
	
	public List<Row> getBRowList(){
		try{
			List<Col> ACols = TableA2B.getTableColsByNames(this.getATable(), this.getAColNames());
			List<Col> BCols = TableA2B.getTableColsByNames(this.getBTable(), this.getBColNames());
			TableA2B.copyColsFromAtoB(ACols, BCols, -1);
			return Row.getRowsFromCols(BCols);
		}catch(Exception e){
			
		}
		return new ArrayList<Row>();
	}
	


}
